package com.cea.papyrus.core.actions.profile;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Profile;

import com.cea.papyrus.core.actions.selectcopypaste.PapyrusSelectionAction;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.utils.DiResourceSet;
import com.cea.papyrus.profile.ui.dialogs.DefinitionHistoryDialog;

/**
 * Action that displays definition history of a profile
 */
public class DisplayDefinitionHistoryAction extends PapyrusSelectionAction {

	/** resource set that contains the resource where the modified Package is */
	protected final DiResourceSet diResourceSet;

	/** request key for the request associated to this action  */
	public final static String DISPLAY_DEFINITION_HISTORY_REQUEST = DisplayDefinitionHistoryAction.class.getName()+ "Request";

	/** ID of  this action */
	public final static String DISPLAY_DEFINITION_HISTORY_ID = DisplayDefinitionHistoryAction.class.getName()+ "ID";

	/**
	 * Creates a new DefineProfileAction
	 * @param part that part which contains the selection
	 */
	public DisplayDefinitionHistoryAction(IWorkbenchPart part) {
		super(part);
		ModelManager manager = (ModelManager)part.getAdapter(ModelManager.class);
		diResourceSet = manager.getDiResourceSet();

		setText("Definition History");
		setId(DISPLAY_DEFINITION_HISTORY_ID);
		setToolTipText("Display the definitions history for this profile");

		// icon for action
		// ImageDescriptor descriptor = PapyrusPlugin.getImageDescriptor(PapyrusPlugin.UML_ICONS_16x16+"Stereotype.gif");
		// setImageDescriptor(descriptor);
		// setHoverImageDescriptor(descriptor);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void run() {
		final Profile profile = getSelectedProfile();
		if(profile == null) {
			return;
		}
		displayDefinitionHistory(profile);
	}

	/**
	 * Execution part of the action
	 * @param profileToDefine the profile for which definition history is displayed
	 */
	public void displayDefinitionHistory(Profile profile) {
		// define profile
		DefinitionHistoryDialog dialog = new DefinitionHistoryDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), profile);
		dialog.open();
	}


	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean calculateEnabled() {
		// look the selection.
		// if one of the element is a package and is not an imported package, it is enabled
		return(getSelectedProfile()!=null);
	}

	/**
	 * returns the first selected profile in the collection of selected elements
	 * @return the first selected profile in the collection of selected elements
	 */
	protected Profile getSelectedProfile() {
		final List<Profile> profiles =getSelectedProfiles(true);
		return (profiles.size()>0) ? profiles.get(0) : null;
	}
	/**
	 * Returns the collection of selected packages 
	 * @param filterImports <code>true</code> if imported elements are counted as valid results
	 * @return the collection of selected packages 
	 */
	protected List<Profile> getSelectedProfiles(boolean filterImports) {
		List<Profile> selectedProfile = new ArrayList<Profile>();
		Iterator<Object> it = getSelectedObjects().iterator();
		while (it.hasNext()) {
			Object object =  it.next();
			if(object instanceof IUMLElementEditPart) {
				IUMLElementEditPart<Element> editPart = (IUMLElementEditPart<Element>)object;
				if(editPart.getUmlElement() instanceof Profile) {
					if(editPart.isElementImport()) {
						if(!filterImports) {
							selectedProfile.add((Profile)editPart.getUmlElement());
						}
					} else {
						selectedProfile.add((Profile)editPart.getUmlElement());
					}
				}
			}
		}
		return selectedProfile;
	}
}
